﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;



namespace CompositeControl
{
    public partial class HightColorLabel : Label
    {
        private string txt;
        private string keyword;
        private Font font;
        private Font keyfont;
        private System.Drawing.Color normalColor = Color.Black;
        private System.Drawing.Color keyColor = Color.Red;
        private System.Drawing.Color backColor = Color.White;
        private SolidBrush sbnormalColor;
        private SolidBrush sbkeyColor;
        private SolidBrush sbbackColor;

        /// <summary>
        /// 显示文本
        /// </summary>
        public string mText
        {
            get { return txt; }
            set
            {
                txt = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 关键词
        /// </summary>
        public string mKeyword
        {
            get { return keyword; }
            set
            {
                keyword = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 字体样式
        /// </summary>
        public Font mFont
        {
            get { return font; }
            set
            {
                font = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 关键词字体样式
        /// </summary>
        public Font mKeyFont
        {
            get { return keyfont; }
            set { keyfont = value; Invalidate(); }
        }

        /// <summary>
        /// 普通字颜色
        /// </summary>
        public Color mNormalColor
        {
            get { return normalColor; }
            set { normalColor = value; Invalidate(); }
        }

        /// <summary>
        /// 关键词颜色
        /// </summary>
        public Color mKeyColor
        {
            get { return keyColor; }
            set
            {
                keyColor = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 背景色
        /// </summary>
        public Color mBackColor
        {
            get { return backColor; }
            set
            {
                backColor = value;
                Invalidate();
            }
        }

        public HightColorLabel()
        {
            InitializeComponent();
            mText = "请设置自己的字符串";
            keyword = "设置";
            font = new System.Drawing.Font("宋体", 15f);
            keyfont = new System.Drawing.Font("黑体", 15f);
            Paint += l_Paint;

        }
        List<string> data = new List<string>();
        //将所给字符串根据关键词 拆分成数组
        private void splitText(string txt)
        {
            if (txt == null || txt.Length < 1)
                return;
            if (txt.Equals(keyword))//当前字符串就是关键词
            {
                data.Add(txt);
                return;
            }
            //拆分文字 拿到关键词
            int startIndex = txt.IndexOf(keyword);//关键词索引
            if (startIndex == -1)//该字符串不包含关键词 直接添加到集合
            {
                data.Add(txt);
                return;
            }
            //包含关键词 还有其他字符串
            int endIndex = startIndex + keyword.Length;
            if (startIndex == 0)//关键词在首（第一次出现 可能后面还包含有关键词）
            {
                string k = txt.Substring(startIndex, endIndex);//拿到关键词
                data.Add(k);
                splitText(txt.Substring(endIndex));
            }
            else if (endIndex == txt.Length)//关键词在末（当前字符串只有一个关键词）
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);
            }
            else//关键词在中
            {
                splitText(txt.Substring(0, startIndex));
                string k = txt.Substring(startIndex, keyword.Length);//拿到关键词
                data.Add(k);
                splitText(txt.Substring(endIndex));
            }
        }

        private void l_Paint(object sender, PaintEventArgs e)
        {
            sbnormalColor = new SolidBrush(normalColor);
            sbkeyColor = new SolidBrush(keyColor);
            sbbackColor = new SolidBrush(backColor);
            Graphics g = e.Graphics;//获取画笔
            //设置背景色
            if (mBackColor != null)
                g.FillRectangle(sbbackColor, 0, 0, this.Width, this.Height);//填充控件大小

            if (mText == null || mText.Length < 1)
            {
                return;
            }

            data.Clear();
            splitText(mText);
            //文字居中显示 dx水平偏移  dy垂直偏移
            //使用Graphics 的 MeasureString（）方法测量指定字体的字符串宽高
            float dx = (this.Width - (g.MeasureString(mText, font)).Width) / 2.0f;
            float dy = (this.Height - (g.MeasureString(mText, font)).Height) / 2.0f;

            if (keyword == null || keyword.Length < 1)//没有关键词  关键词为空
            {
                g.DrawString(mText, font, sbnormalColor, dx, dy);
                return;
            }

            //拆分文字 拿到关键词
            int startIndex = mText.IndexOf(keyword);//关键词索引
            int endIndex = startIndex + keyword.Length;

            string[] chunks = data.ToArray();

            //当设置了关键词字体时  需要根据该字体测量 偏移量
            if (keyfont != null)
            {
                float ddx = 0;
                float ddy = 0;
                ddx = this.Width;
                //遍历
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddx -= g.MeasureString(keyword, keyfont).Width;
                    }
                    else
                    {
                        ddx -= g.MeasureString(chunks[i], font).Width;
                    }
                }
                ddx = ddx / 2.0f;
                if (ddx < 0)
                    ddx = 0;
                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        ddy = (this.Height - (g.MeasureString(keyword, keyfont)).Height) / 2.0f;
                        g.DrawString(chunks[i], keyfont, sbkeyColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], keyfont)).Width;
                    }
                    else
                    {
                        ddy = (this.Height - (g.MeasureString(chunks[i], font)).Height) / 2.0f;
                        g.DrawString(chunks[i], font, sbnormalColor, ddx, ddy);
                        //水平偏移自增
                        ddx += (g.MeasureString(chunks[i], font)).Width;
                    }
                }
            }
            else
            {
                //遍历着色 
                for (int i = 0; i < chunks.Length; i++)
                {
                    if (chunks[i].Equals(keyword))//关键词
                    {
                        g.DrawString(chunks[i], font, sbkeyColor, dx, dy);
                    }
                    else
                    {
                        g.DrawString(chunks[i], font, sbnormalColor, dx, dy);
                    }
                    //水平偏移自增
                    dx += (g.MeasureString(chunks[i], font)).Width;
                }
            }
        }
    }
}
